﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;

namespace OT.Areas.Instructor.Controllers
{
    public class SubjectController : Controller
    {
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex, string columnFilter)
        {
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            else
            {
                TempData["notice"] = "Time Out ,Please Login";
                Response.Redirect("~/Home");
            }
            var Subject = (List<Subjects>)null;
            int recount = 0;

            try
            {
                Subject = (from c in OTDb.Subjects
                           where c.LanguageID == LanguageID
                           select new Subjects
                           {
                               SubjectID = c.SubjectID,
                               SubjectName = c.SubjectName,
                               Description = c.Description,
                               CreatedDate = c.CreatedDate.Value == null ? DateTime.MinValue : c.CreatedDate.Value,
                               UpdatedDate = c.UpdatedDate.Value == null ? DateTime.MinValue : c.UpdatedDate.Value,
                               Status = (bool)c.Status,
                               CategoryName = c.Category.CategoryName
                           }).ToList();

                if (!String.IsNullOrEmpty(searchString))
                    Subject = Subject.Where(r => r.SubjectName.ToLower().Contains(searchString.ToLower())).ToList();

                recount = Subject.Count;

                Subject = Subject.Skip<Subjects>(pgindex * (pageNumber - 1))
                    .Take<Subjects>(pgindex).ToList<Subjects>();

            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Subject,
                RecordCount = recount
            });
        }

        public ActionResult Create()
        {
            Subjects Stats = null;
            try
            {
                Stats = new Subjects();
                var Country = OTDb.Categories.Where(c=>c.Status==true).ToList();
                Stats.categorys = Country;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Stats);
        }

        [HttpPost]
        public string Create(string SubjectName, string Description, int CategoryID, bool Status)
        {
            string result = "";

            Subject Subject = null;
            try
            {
                if (ModelState.IsValid)
                {
                    int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value); ;
                    Subject = new Subject();
                    Subject.SubjectName = SubjectName;
                    Subject.Description = Description;
                    Subject.Status = Status;
                    Subject.UserID = UserID;
                    Subject.CreatedDate = DateTime.Now;
                    Subject.UpdatedDate = DateTime.Now;
                    Subject.LanguageID = LanguageID;
                    if (CategoryID != 0)
                        Subject.CategoryID = CategoryID;
                    OTDb.Subjects.Add(Subject);
                    OTDb.SaveChanges();
                    result = "Success";
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }

        public ActionResult Edit(int SubjectID)
        {
            Subjects Subjects = null;
            try
            {
                var Subject = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                if (Subject != null)
                {

                    Subjects = new Subjects();
                    var category = OTDb.Categories.ToList();
                    Subjects.categorys = category;
                    Subjects.SubjectID = Subject.SubjectID;
                    Subjects.SubjectName = Subject.SubjectName;
                    Subjects.Description = Subject.Description;
                    Subjects.CategoryID =Subject.CategoryID;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Subjects);
        }
        [HttpPost]
        public string Edit(int SubjectID, string SubjectName, string Description, int CategoryID)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var Subject = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                    if (Subject != null)
                    {
                        Subject.SubjectName = SubjectName;
                        Subject.Description = Description;
                        Subject.UpdatedDate = DateTime.Now;
                        Subject.CategoryID = (int)CategoryID;
                        OTDb.SaveChanges();
                        result = "Success";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        public ActionResult Delete(int SubjectID)
        {
            Subjects Subjects = null;
            try
            {
                var Subject = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                if (Subject != null)
                {
                    Subjects = new Subjects();
                    Subjects.SubjectID = Subject.SubjectID;
                    Subjects.SubjectName = Subject.SubjectName;
                    Subjects.Description = Subject.Description;
                    Subjects.Status = (bool)Subject.Status;
                    if (Subject.CategoryID != null)
                        Subjects.CategoryID = (byte)Subject.CategoryID;
                    var category = OTDb.Categories.ToList();
                    Subjects.categorys = category;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Subjects);
        }

        [HttpPost]
        public string Delete(int SubjectID,string IsDelete)
        {
            string result = "";
            try
            {
                var Subject = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                if (Subject != null)
                {
                    OTDb.Subjects.Remove(Subject);
                    OTDb.SaveChanges();
                    result = "Success";
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
                result = "NotValid";
            }
            return result;
        }
    }
}
